<template>
  <section>
    <div class="tapDemand-panel">
      <el-row class="options-tool">
        <el-button @click="printPDF" type="success">下载</el-button>
      </el-row>
      <div class="panel" ref="pdfContent">
        <div class="title">
          {{deptName}}抽调需求表（{{ detail.batchNo }}）
        </div>
        <div class="table">
          <el-table :data="tableData" :border="true">
            <el-table-column
              label="年度组号"
              align="center"
              prop="yearGroup"
              width="180px"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.yearGroup"
                  placeholder="请输入年度组号"
                  clearable
                  :disabled="row.isView || row.isAuth"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="检查时间"
              align="center"
              prop="checkData"
              width="260px"
            >
              <template slot-scope="scope">
                <el-date-picker
                  v-model="scope.row.checkData"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :disabled="row.isView || row.isAuth"
                ></el-date-picker>
              </template>
            </el-table-column>
            <el-table-column
              label="检查事项"
              align="center"
              prop="applyReason"
            />
            <el-table-column
              label="检查企业"
              align="center"
              prop="vendorName"
              width="260px"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.vendorName"
                  type="textarea"
                  rows="1"
                  :disabled="row.isView || row.isAuth"
                  placeholder="请输入检查企业"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="检查地点"
              align="center"
              prop="checkAddress"
              width="260px"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.checkAddress"
                  type="textarea"
                  rows="1"
                  :disabled="row.isView || row.isAuth"
                  placeholder="请输入检查地点"
                />
              </template>
            </el-table-column>
            <el-table-column label="检查范围" align="center" prop="checkRange">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.checkRange"
                  type="textarea"
                  rows="1"
                  :disabled="row.isView || row.isAuth"
                  placeholder="请输入检查范围"
                />
              </template>
            </el-table-column>
            <el-table-column label="检查人数" align="center" prop="checkNumber">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.checkNumber"
                  type="number"
                  :disabled="row.isView || row.isAuth"
                  @input="onInput(scope.row.checkNumber, scope.$index)"
                  placeholder="请输入检查人数"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="科室经办人"
              align="center"
              prop="operatorName"
              width="120px"
            />
          </el-table>
        </div>
        <Process
          :processData="processData"
          v-if="row.isView && processData.length"
        />
      </div>

      <div class="form-panel collect">
        <Form
          :formData="formData"
          :formBtns="formBtns"
          @handleSubmit="handleSubmit"
        />
      </div>
    </div>
    <Audit
      @onBack="
        () => {
          this.isAudit = false;
        }
      "
      @onSuccess="onSuccess"
      v-if="isAudit"
    />
  </section>
</template>

<script>
import Form from "@/components/form/index.vue";
import Audit from "../audit/index.vue";
import Process from "../audit/process.vue";
import { storage } from "@/utils/util.js";
import { listDept } from "@/api/system/dept";
import {
  vendorRequirementsGetInfo,
  vendorRequirementsResponsibilityAudit,
  vendorRequirementsOperatorAudit,
  vendorRequirementsUpdate,
} from "@/api/collect/tapDemand/index";
export default {
  components: {
    Form,
    Audit,
    Process,
  },
  props: {
    row: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      detail: {},
      // 是否提交审核
      isAudit: false,
      id: "",
      deptName:'',
      tableData: [{ createTime: "" }],
      user: storage.getStorage("user") || {},
      processData: [],
      formData: [
        {
          prop: "status",
          type: "select",
          options: [
            {
              label: "通过",
              value: "1",
            },
            {
              label: "拒绝",
              value: "0",
            },
          ],
          label: "审批结果",
          value: "1",
        },
        {
          prop: "reason",
          type: "textarea",
          label: "审批意见",
          rows: 4,
        },
      ],
      formBtns: [
        //表单按钮
        {
          type: "primary",
          label: "提交审核",
          isVerify: true,
        },
        {
          type: "success",
          label: "保存",
          hidden: this.row.isAuth,
          click: () => {
            let tableData = [...this.tableData];
            tableData.forEach((item) => {
              if (item.checkData && item.checkData.length) {
                item.checkStartDate = item.checkData[0];
                item.checkEndDate = item.checkData[1];
                delete item.checkData;
                item.batchNo = this.row.batchNo;
              }
            });
            vendorRequirementsUpdate(tableData).then((res) => {
              this.Message("保存成功");
              this.$emit("onSuccess");
            });
          },
        },
        {
          label: "返回",
          click: () => {
            this.$emit("onBack");
          },
        },
      ],
    };
  },
  created() {
    if (this.$route.query.isView || this.row.isView) {
      this.formData = [];
      this.formBtns = [
        {
          label: "返回",
          click: () => {
            this.$emit("onBack");
          },
        },
      ];
    }
    if (!this.row.isView && !this.row.isAuth) {
      this.formData = [];
    }
  },
  mounted() {
    this.getDeptList();
    if (this.row.batchNo) {
      this.getLoad();
    }
  },
  methods: {
    //获取数据
    getLoad() {
      vendorRequirementsGetInfo({ batchNo: this.row.batchNo }).then((res) => {
        this.detail = res.data;
        let vendorInfoList = res.data.vendorInfoList || [];
        this.processData = this.detail.auditList || [];
        if (vendorInfoList.length) {
          vendorInfoList.forEach((item) => {
            this.$set(
              item,
              "checkData",
              item.checkStartDate
                ? [item.checkStartDate, item.checkEndDate]
                : []
            );
          });
        }
        this.tableData = vendorInfoList;
      });
    },


    /** 查询科室列表 */
    getDeptList() {

      listDept({}).then(response => {
         this.deptName=response.data.filter(item=>item.deptId==this.user.deptId)[0].deptName;
      });
    },

    //保存
    handleSubmit(data) {
      this.isAudit = true;
      if (this.row.isAuth) {
        let params = {
          ...data,
          batchNo: this.row.batchNo,
        };
        vendorRequirementsResponsibilityAudit(params).then((res) => {
          this.Message("审核成功");
          this.row = data;
          this.isAudit = true;
          this.$emit("onSuccess");
        });
      }
    },
    //审核回调
    onSuccess(data) {
      let array = data.nextAudit.split("/");
      let tableData = [...this.tableData];
      tableData.forEach((item) => {
        if (item.checkData && item.checkData.length) {
          item.checkStartDate = item.checkData[0];
          item.checkEndDate = item.checkData[1];
          delete item.checkData;
        }
      });
      let params = {
        nextAuditId: array[0],
        nextAuditName: array[1],
        batchNo: this.row.batchNo,
        vendorInfoList: tableData,
      };
      vendorRequirementsOperatorAudit(params).then((res) => {
        this.Message("审核成功");
        this.$emit("onSuccess");
      });
    },
    //下载pdf
    printPDF() {
      this.PrintPDF("抽调需求表", this);
    },

    //检查人数校验
    onInput(e, index) {
      let value = parseFloat(e);
      let flag =
        typeof value === "number" && value > 0 && Math.floor(value) === value;
      if (!flag) {
        this.Message("请输入正整数", "error");
        this.tableData[index].checkNumber = "";
      }
    },
  },
};
</script>

<style lang="scss">
@import "../../../assets/styles/collect.scss";
.tapDemand-panel {
  padding: 30px;
  .title {
    padding: 20px;
    font-size: 22px;
    text-align: center;
  }
}
</style>
